//
//  HealthViewController.m
//  liangyi
//
//  Created by 张水 on 16/12/15.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "HealthViewController.h"
#import "DVSwitch.h"
#import "AppHeader.h"
#import "LeftCell.h"
#import "RIghtCell.h"
#import "ZXVideo.h"
#import "VideoPlayViewController.h"
#import "MadePlanViewController.h"
#import "DetailVideoViewController.h"
#import "KnowWebViewVC.h"
#import "PlanModel.h"
#import "SchemeViewController.h"
#import "CustomScrollView.h"
@interface HealthViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) CustomScrollView *scrollView;

@property(nonatomic,strong) UITableView  *LeftVC;

@property(nonatomic,strong) UITableView  *RightVC;

@property(nonatomic,strong)DVSwitch  *navSwitch;

@property(nonatomic,assign)int *selectIndex;

@property(nonatomic,strong) UIButton *scuess;

@property (assign, nonatomic) NSInteger index;

@property(assign,nonatomic)NSInteger pageNo;

@property(nonatomic,strong) NSMutableArray *leftArr;

@property(nonatomic,strong) NSMutableArray *rightArr;

@end

@implementation HealthViewController

static NSString *leftID = @"LeftCell";
static NSString *rightID = @"RIghtCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNo = 1;
    self.leftArr = [NSMutableArray array];
    self.rightArr = [NSMutableArray array];
    [self BuildScrollView];
    [self setupNavigation];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self LoadDisease];
}

-(void)BuildScrollView{
    
    self.scrollView = [[CustomScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.directionalLockEnabled = YES;
    self.scrollView.contentSize = CGSizeMake( 2 * kScreenWidth, kScreenHeight - 64 );
    [self.view addSubview:self.scrollView];
    
    self.LeftVC =[[UITableView  alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.LeftVC.dataSource = self;
    self.LeftVC.delegate = self;
    self.LeftVC.scrollEnabled = YES;
    self.LeftVC.separatorStyle =  UITableViewCellSeparatorStyleNone;
    [self.LeftVC registerClass:[LeftCell class] forCellReuseIdentifier:leftID];
    [CommonMethod hiddleExtendCellFromTableview:self.LeftVC];
    [self.scrollView addSubview:self.LeftVC];
    self.LeftVC.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    self.RightVC =[[UITableView  alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)  style:UITableViewStylePlain];
    self.RightVC.dataSource = self;
    self.RightVC.delegate = self;
    self.RightVC.scrollEnabled = YES;
    self.RightVC.separatorStyle =  UITableViewCellSeparatorStyleNone;
    [self.RightVC registerClass:[RIghtCell class] forCellReuseIdentifier:rightID];
    [CommonMethod hiddleExtendCellFromTableview:self.RightVC];
    [self.scrollView addSubview:self.RightVC];
    self.RightVC.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    
}

-(void)setupNavigation{
    
    
    self.navSwitch = [DVSwitch switchWithStringsArray:@[@"康复",@"科普"]];
//    self.navSwitch.frame = CGRectMake(10, 10, 140, 40);
    self.navSwitch.frame = CGRectMake(10, 10, 140, 30);
//    self.navSwitch.layer.shadowColor = [UIColor ColorWithHex:@"#2c909b"].CGColor;
//    self.navSwitch.layer.shadowOffset = CGSizeMake(0, -1);
//    self.navSwitch.layer.shadowOpacity = 3;
//    self.navSwitch.layer.shadowRadius = 5;
    self.navSwitch.cornerRadius = 15;
    self.navSwitch.backgroundColor = [UIColor ColorWithHex:@"#41a8b4"];
    self.navSwitch.labelTextColorInsideSlider = [UIColor whiteColor];
    self.navSwitch.labelTextColorOutsideSlider = [UIColor ColorWithHex:@"#b7f6fe"];
    self.navSwitch.sliderColor = [UIColor ColorWithHex:@"#369ca6"];
    self.navigationItem.titleView = self.navSwitch;
    
    self.selectIndex = 0;
    __weak HealthViewController *weakSelf = self;
    [self.navSwitch setPressedHandler:^(NSUInteger index) {
        if (index == 0){
           weakSelf.scrollView.contentOffset = CGPointMake(0, -64);
            [weakSelf LoadDisease];
        }else{
            weakSelf.scrollView.contentOffset = CGPointMake(kScreenWidth, -64);
              [weakSelf LoadKnowData];
        }
    }];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offset = self.scrollView.contentOffset.x;
    [self.navSwitch forceSelectedIndex:offset/kScreenWidth animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.RightVC == tableView){
        return  self.rightArr.count;
    }
    return self.leftArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.RightVC == tableView){
        RIghtCell *rightcell = [tableView dequeueReusableCellWithIdentifier:rightID forIndexPath:indexPath];
        if (!rightcell){
            rightcell = [rightcell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rightID];
        }
        rightcell.selectionStyle =  UITableViewCellSelectionStyleNone;
        rightcell.rightModel = self.rightArr[indexPath.row];
        return  rightcell;
    }else{
        LeftCell *leftcell = [tableView dequeueReusableCellWithIdentifier:leftID forIndexPath:indexPath];
        if (!leftcell){
            leftcell = [leftcell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:leftID];
        }
        leftcell.leftModel = self.leftArr[indexPath.row];
        return leftcell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.RightVC == tableView){
      return  50;
    }
    return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.RightVC == tableView){
        
            SchemeViewController *vc = [[SchemeViewController alloc]init];
            vc.model = self.rightArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        
            KnowWebViewVC *webVC = [[KnowWebViewVC alloc]init];
            webVC.model = self.leftArr[indexPath.row];
            [self.navigationController pushViewController:webVC animated:YES];
    }
    
}

-(void)LoadDisease{
    
    NSString *url = [BASEURL stringByAppendingString:kURL_SchemeList];
    NSDictionary *dict = @{@"cateId":self.mainModel.id};
    NSLog(@"%@",dict);
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
                [self.rightArr removeAllObjects];
                NSArray *arr = [RightModel mj_objectArrayWithKeyValuesArray:network.dataObject];
                NSLog(@"xxx%lu",(unsigned long)arr.count);
                [self.rightArr addObjectsFromArray:arr];
                NSLog(@"%ld",(unsigned long)self.rightArr.count);
                [self.RightVC reloadData];
            
            
        }
    } failure:^(id error) {
        
    }];
    
}
-(void)LoadKnowData{
    
    NSString *url = [BASEURL stringByAppendingString:kURL_KnowList];
    NSDictionary *dict = @{@"cateId":self.mainModel.id,
                           @"pageNo":[NSString stringWithFormat:@"%ld",(long)self.pageNo]};
    NSLog(@"%@",dict);
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
         NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            if ([network.dataObject isKindOfClass:[NSArray class]]){
                [self.leftArr removeAllObjects];
                NSArray *arr = [LeftModel mj_objectArrayWithKeyValuesArray:network.dataObject];
                [self.leftArr addObjectsFromArray:arr];
                NSLog(@"%ld",(unsigned long)self.leftArr.count);
                [self.LeftVC reloadData];
            }
        }
    } failure:^(id error) {
        
    }];

    
}
@end
